﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Code.Search;
using Code.ProductType;
using Code.Manufacture;
using E2_WEBSITE.Code.Product;
using OboutInc.Window;
using Obout.Grid;

public partial class Search_advance_search : System.Web.UI.Page
{
    private string m_sPostBackProductType;
    private string m_sPostBackManufacture;
    private int m_iPostBackBeginMoney;
    private int m_iPostBackEndMoney;
    protected void Page_Load(object sender, EventArgs e)
    {
        BindingTableResult(null);
        lbMessage.Text = "";

       
        if (!Page.IsPostBack)
        {        
            Window myWindow = this.Master.FindControl("ContentPlaceHolder1").FindControl("myWindow") as Window;
            myWindow.PageOpacity = 30;
            myWindow.IsDraggable = false;
            myWindow.IsResizable = false;
         
        }
        else
        {

           
        }
   
        
    }

    void Page_LoadComplete(object sender, EventArgs e)
    {
        SearchController sController = new SearchController();
        if (IsPostBack)
        {
            if (cbProductType.SelectedIndex > 0)
                m_sPostBackProductType = cbProductType.Text;
            else
                m_sPostBackProductType = "";
            if (cbManufacture.SelectedIndex > 0)
                m_sPostBackManufacture = cbManufacture.Text;
            else
                m_sPostBackManufacture = "";

            m_iPostBackBeginMoney = cbBeginPrice.SelectedIndex * 1000000;

            m_iPostBackEndMoney = cbEndPrice.SelectedIndex * 1000000;

            string strConName = tbProductName.Text;
            if (strConName != "")
            {
                DataTable dt = sController.DoSearch(strConName);
                if (dt != null && dt.Rows.Count > 0)
                {
                    BindingTableResult(dt);
                }
                else
                {
                    lbMessage.Text = "Không tìm được kết quả phù hợp";
                }
            }
            else
            {

                
                //cbProductType.Text = m_sPostBackProductType;
                //cbManufacture.Text = m_sPostBackManufacture;
                /////////////
                DataTable dt = sController.DoAdvanceSearch(m_sPostBackProductType, m_sPostBackManufacture, m_iPostBackBeginMoney, m_iPostBackEndMoney);
                /////////////////////
                if (dt != null && dt.Rows.Count > 0)
                {
                    BindingTableResult(dt);
                }
                else
                {
                    lbMessage.Text = "Không tìm được kết quả phù hợp";
                }
            }
        }
        else
        {
            ProductTypeController ptc = new ProductTypeController();
            List<string> lstProductTypeName = ptc.GetListProductTypeName();
            cbProductType.Items.Clear();
            cbProductType.Items.Add("- Chọn Loại Sản Phẩm -");
            foreach (string pInfo in lstProductTypeName)
            {
                cbProductType.Items.Add(pInfo);
            }

            ManufactureController mc = new ManufactureController();
            List<string> lsManufactureName = mc.GetListManufactureName();
            cbManufacture.Items.Clear();
            cbManufacture.Items.Add("- Chọn Nhà Sản Xuất -");
            foreach (string mInfo in lsManufactureName)
            {
                cbManufacture.Items.Add(mInfo);
            }
      
            string str = Request.QueryString["SearchCon"];
            if (str != null)
            {
                ////////////////////////////
                DataTable dt = sController.DoSearch(str);
                if (dt != null && dt.Rows.Count > 0)
                {

                    BindingTableResult(dt);
                }
                else
                {
                    lbMessage.Text = "Không tìm được kết quả phù hợp";
                }
            }
            else
            {
                string sProductType = Request.QueryString["ProType"];
                if (sProductType != null)
                {
                    int iBeginPrice = Int32.Parse(Request.QueryString["BegP"]) * 1000000;
                    int iEndPrice = Int32.Parse(Request.QueryString["EndP"]) * 1000000;
                    //////////////////
                    DataTable dt = sController.DoAdvanceSearch(sProductType, "", iBeginPrice, iEndPrice);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        BindingTableResult(dt);
                    }
                    else
                        lbMessage.Text = "Không tìm được kết quả phù hợp";
                }

            }

        }
    }

    private void BindingTableResult(DataTable dt)
    {
        Grid grid1 = this.Master.FindControl("ContentPlaceHolder1").FindControl("grid1") as Grid;

        grid1.DataSource = dt;
        grid1.DataBind();
    }
        

}
